package com.xiaoxi.open.codemodel.core.impl;

import com.xiaoxi.open.codemodel.core.ICode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author xiaoxi
 * @ProjectName springcloudbms
 * @PackageName com.xiaoxi.open.codemodel.core.impl
 * @ClassName AbstractCodeServiceImpl
 * @Description TODO
 * @Date 2023/2/22 20:37
 * @Version 1.0
 */
@Slf4j
public abstract class AbstractCodeServiceImpl implements ICode<Map<String, Object>> {

    public Map<String, Object> generatorCondition() {

        Map<String, Object> condition = new HashMap<>();
//        BMSUserContext currentUser = AuthUtils.getCurrentUser();
//
//        condition.put("id", currentUser.getId());
//        condition.put("username", currentUser.getUsername());

        // 自定义请求参数
        HttpServletRequest request = getHttpRequest();
        if (Objects.nonNull(request)) {
            Map<String, String[]> parameters = request.getParameterMap();
            if (Objects.nonNull(parameters)) { //放入其它参数
                for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
                    String key = entry.getKey();
                    String[] values = entry.getValue();
                    if (values != null && values.length > 0) {
                        condition.put(key, values.length > 1 ? values : values[0]);
                    }
                }
            }
        }

        return condition;
    }

    private HttpServletRequest getHttpRequest() {
        return RequestContextHolder.getRequestAttributes() == null ? null : ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

}
